package com.api.customization.unionpay.market.web;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.ClearFundDetailsButtonService;
import com.customization.unionpay.market.service.impl.ClearFundDetailsButtonServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * @author sjm
 * @description 清算资金明细确认数据
 */
@Path("/market/clear/details")
public class ClearFundDetailsConfirmDataController {

    private static final Logger log = LoggerFactory.getLogger(ClearFundDetailsConfirmDataController.class);

    private ClearFundDetailsButtonService getService(User user) {
        return ServiceUtil.getService(ClearFundDetailsButtonServiceImpl.class, user);
    }

    @POST
    @Path("/fund")
    @Produces(MediaType.APPLICATION_JSON)
    public String clearFundDetailsConfirmData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "执行成功！");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            String body = FormDataUtils.readBodyUTF8(request);
            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);
                String ids = parseJson.getString("ids");
                if (ids != null && !"".equals(ids)) {
                    String resultStr = getService(user).comfirmDataDetailsInfo(ids, user);
                    if(resultStr != null){
                        result.put("msg",resultStr);
                        result.put("code", 500);
                        result.put("errorCode", 10003);
                    }
                } else {
                    result.put("msg", "请选择确认数据！");
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                }
            } else {
                result.put("msg", "请求体参数为空！");
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", e.getMessage());
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

}
